package com.example.group.network.express_plus_app.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.group.network.express_plus_app.MainApplication;
import com.example.group.network.express_plus_app.R;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.http.GsonArrayRequest;
import com.example.group.network.express_plus_app.http.MySingleton;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/4/5 11:09
 */
public class MyOrderHistoryViewModel extends AndroidViewModel {
    String server_ip ;
    private MutableLiveData<ArrayList<Order>> orderMutableLiveData;
    RequestQueue queue;

    public MyOrderHistoryViewModel(@NonNull Application application) {
        super(application);
        orderMutableLiveData = new MutableLiveData<ArrayList<Order>>();
        queue= MySingleton.getInstance(application).getRequestQueue();
        server_ip=getApplication().getString(R.string.server_ip);
    }
    public LiveData<ArrayList<Order>> getMyOrderHistory() {
        return orderMutableLiveData;
    }
    //根据手机号和token查找我刚发布但还没有被接单的订单
    public void findOrderHistory(){

        HashMap<String, String> mInfoMap = MainApplication.getInstance().mInfoMap;
        String phone = mInfoMap.get("phone");
        String token = mInfoMap.get("token");
        String url=server_ip+"express-order/order/find-history-common-user-order-by-phone?userPhone="
                +phone;
        HashMap<String, String> heads = new HashMap<>();
        heads.put("Authorization","Bearer "+token);
        System.out.println(server_ip);
        GsonArrayRequest gsonArrayRequest=new GsonArrayRequest(url, Order.class, heads, new Response.Listener<ArrayList<Order>>() {
            @Override
            public void onResponse(ArrayList<Order> response) {
                orderMutableLiveData.setValue(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("出错", String.valueOf(error.getNetworkTimeMs()));
            }
        });

        gsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(gsonArrayRequest);

    }
}
